from flask_restful import Resource
from restful.api__.public__.service.LoginOrRegister import DelUser
from restful.api__.session import  admin_required_current_admin_generate, \
    uid_required_target_user_generate
from restful.model.query import QueryOneInstance
from restful.model.tables import Role
from restful.response import ResponseSuccess, ResponseConflict, ResponseForbidden


class UserDelete(Resource):
    @admin_required_current_admin_generate
    @uid_required_target_user_generate
    def delete(self,target_user,current_admin):
        role = QueryOneInstance(Role,{'id':target_user.id})

        if not role:
            return ResponseConflict("不知用户身份，无法删除")

        if role.admin:
            return ResponseForbidden("用户为管理员，不可删除")

        DelUser(target_user.id)
        return ResponseSuccess(msg=f"删除uid:{target_user.id}的用户成功")